package com.sunmouse.toollib.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sunmouse.toollib.R;

/**
 * Created by Kvin on 2017/6/8.
 */
public class ProgressUtils {
    private TextView mCursorTv;
    private TextView mDurationTv;
    private TextView mTitleTv;
    private ProgressBar mProgressBar;

    private static AlertDialog mDialog;


    private ProgressUtils(View v, Context context) {
        this.mCursorTv = (TextView) v.findViewById(R.id.cursor_tv);
        this.mDurationTv = (TextView) v.findViewById(R.id.duration_tv);
        this.mTitleTv = (TextView) v.findViewById(R.id.title_tv);
        this.mProgressBar = (ProgressBar) v.findViewById(R.id.m_progress);

        //build dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        builder.setView(v);
        mDialog = builder.create();
        mDialog.getWindow().setWindowAnimations(R.style.Transition_TopInTopOut);

        mDialog.onAttachedToWindow();
    }

    /**
     * get an instance
     */
    public static ProgressUtils getInstance(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
        return new ProgressUtils(v, context);
    }

    /**
     * set layout params
     *
     * @param w
     * @param h
     */
    public void setLayoutParams(int w, int h) {
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = w;
        lp.height = h;
        setLayoutParams(lp);
    }

    public void setLayoutParams(WindowManager.LayoutParams lp) {
        if (mDialog == null) return;
        mDialog.getWindow().setAttributes(lp);
    }

    /**
     * set outside click event
     */
    public void setCanceledOnTouchOutside(boolean tag) {
        if (mDialog == null) return;
        mDialog.setCanceledOnTouchOutside(tag);
    }

    /**
     * show or hide
     */
    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    /**
     * set text
     */
    public void setTitle(String title) {
        this.mTitleTv.setText(TextUtils.isEmpty(title) ? "" : title);
    }

    public void setCursor(String title) {
        this.mCursorTv.setText(TextUtils.isEmpty(title) ? "" : title);
    }

    public void setDuration(String txt) {
        this.mDurationTv.setText(txt);
    }

    public void setMax(int max) {
        this.mProgressBar.setMax(max);
    }

    public int getMax() {
        return this.mProgressBar.getMax();
    }

    public void setProgress(int progress) {
        this.mProgressBar.setProgress(progress);
        this.setCursor(progress * 100 / mProgressBar.getMax() + "%");
        this.setDuration(progress + "/" + mProgressBar.getMax());
    }

    public int getProgress() {
        return this.mProgressBar.getProgress();
    }

    public void setOutCanceled(boolean cancel) {
        this.mDialog.setCanceledOnTouchOutside(cancel);
    }


    /**
     * set transition animation
     */
    public void setAnim(@StyleRes int anim) {
        if (mDialog == null) return;
        mDialog.getWindow().setWindowAnimations(anim);
    }


}
